package cn.edu.ctbu.yrx.service;

import cn.edu.ctbu.yrx.dao.TeacherRepository;
import cn.edu.ctbu.yrx.entity.Student;
import cn.edu.ctbu.yrx.entity.Teacher;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    public TeacherRepository teacherRepository;


    public List<Teacher> getAll() {

//        return studentRepository.findAll();
        return teacherRepository.findAllWithUser();
    }

    public Page<Teacher> getAll(Pageable pageable) {
        return teacherRepository.findAll(pageable);
    }

    public Page<Teacher> getAll(Example<Teacher> example, Pageable pageable) {
        return teacherRepository.findAll(example, pageable);
    }

    public Teacher findByName(String name) {
        return teacherRepository.findByTeacherName(name).get(0);
    }

    public Teacher findById(int id) {
        return teacherRepository.findById(id).get();
    }

    public Teacher getById(Integer id) {
        return teacherRepository.findById(id).orElse(null);
    }

    /**
     * 新增学生
     *
     * @param teacher
     * @return
     */
    public Teacher add(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /**
     * 更新学生
     *
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /**
     * 删除学生
     *
     * @param ids
     */
//    public void delete(Integer id) {
//        studentRepository.deleteById(id);
//    }
    @Transactional
    public void deletebatch(Iterable<Integer> ids) {
        teacherRepository.deleteAllByIdInBatch(ids);
    }
}
